package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ww.yaodao.mapper.ProductMapper;
import com.ww.yaodao.mapper.TypeMapper;
import com.ww.yaodao.model.bo.ProductDO;
import com.ww.yaodao.model.bo.TypesDO;
import com.ww.yaodao.model.bo.UserDO;
import com.ww.yaodao.service.ProductService;
import com.ww.yaodao.model.vo.ProductPageVO;
import com.ww.yaodao.model.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List queryAllProduct() {
        List<ProductDO> productDOS = productMapper.selectList(null);
        return productDOS;
    }


    @Override
    public int updateProduct(ProductVO productVo) {
        Integer tid = getTid(productVo.getPtype());
        ProductDO productDO = new ProductDO();
        productDO.setPcount(productVo.getPcount());
        productDO.setPdes(productVo.getPdes());
        productDO.setPname(productVo.getPname());
        productDO.setPprice(productVo.getPprice());
        productDO.setPtype(tid);
        LambdaQueryWrapper<ProductDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDO::getPid,productVo.getPid());
        int update = productMapper.update(productDO, wrapper);
        return update;
    }

    @Override
    public int deleteProductById(Integer id) {
        QueryWrapper<ProductDO> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("pid", id);
        int i = productMapper.delete(productQueryWrapper);
        return i;
    }



    @Override
    public Page<ProductDO> pageProduct(Integer current, Integer size, ProductPageVO productVo) {
        //将前端传递的类别进行转换
        Integer tid = getTid(productVo.getPtype());
        //在药品信息表中查询
        Page<ProductDO> page = new Page<>(current, size);
        LambdaQueryWrapper<ProductDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(productVo.getPname())) {
            queryWrapper.like(ProductDO::getPname, productVo.getPname());
        }
        if (!StringUtils.isEmpty(productVo.getPtype())) {
            queryWrapper.like(ProductDO::getPtype, tid);
        }
        if (productVo.getStartPrice() != null) {
            queryWrapper.ge(ProductDO::getPprice, productVo.getStartPrice());
        }
        if (productVo.getEndPrice() != null) {
            queryWrapper.ge(ProductDO::getPprice, productVo.getEndPrice());
        }
        Page<ProductDO> productDOPage = productMapper.selectPage(page, queryWrapper);
        return productDOPage;
    }
    public Integer getTid(String type){
        LambdaQueryWrapper<TypesDO> typesDOQueryWrapper = new LambdaQueryWrapper<>();
        TypesDO typesDO = null;
        if (type!=""){
            typesDOQueryWrapper.like(TypesDO::getTid,type);
            typesDO = typeMapper.selectOne(typesDOQueryWrapper);
            return typesDO.getTid();
        }else{
            return null;
        }

    }
}
